package csc.daonjpa.java.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Component;

import csc.daonjpa.java.domain.Account;
import csc.daonjpa.java.transaction.TransactionManager;

/**
 * DAO class for Account entity. This class contains all methods that inserts/updates/deletes
 * account info
 * @author nvu3
 *
 */
@Component
public class AccountDAO {
	
	/**
	 * @return all accounts from Account table
	 */
	public List<Account> getAccounts() {
		EntityManager em = TransactionManager.getEntityManager();
		TypedQuery<Account> query = em.createQuery("SELECT a FROM Account a", Account.class);
		List<Account> accounts = null;
		accounts = query.getResultList();
		return accounts;
	}
	
	/**
	 * Add account to Account table
	 * @param account Account entity that contains mapped with each column in Account table
	 * @return
	 */
	public boolean addAccount(Account account) {
		EntityManager em = TransactionManager.getEntityManager();
		em.persist(account);
		return true;
	}
}
